package com.odd.webapp.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.odd.webapp.businessobjects.*;

/**
 * TODO FieldEditPanel class-level Javadoc.
 *
 * <p>...</p>
 *
 * @author Copyright (c) CHP Consulting Ltd. 2010
 */
public class FieldEditPanel extends Composite {

  private static FieldEditPanelUiBinder uiBinder = GWT.create(FieldEditPanelUiBinder.class);

  interface FieldEditPanelUiBinder extends UiBinder<Widget, FieldEditPanel> {
  }

  @UiField
  TextBox   categoryName;
  @UiField
  TextBox   fieldName;
  @UiField
  TextBox   fieldType;
  @UiField
  TextBox   fieldLength;
  @UiField
  TextBox   fieldDecimalLength;
  @UiField
  TextBox   fieldDescription;


  public FieldEditPanel(String categoryName) {
    initWidget(uiBinder.createAndBindUi(this));
    this.categoryName.setText(categoryName);
  }


  @UiHandler("buttonSave")
  void doClick(ClickEvent e) {
    
    Field f1 = new Field();
    f1.setCategoryName(categoryName.getText());
    f1.setName(fieldName.getText());
    f1.setType(fieldType.getText());
    f1.setLength(Integer.parseInt(fieldLength.getText()));
    f1.setDecimalLength(Integer.parseInt(fieldDecimalLength.getText()));
    f1.setDescription(fieldDescription.getText());
    
    Open_data_dictionary.getService().createField(f1, new AsyncCallback() {

      public void onFailure(Throwable caught) {
        GWT.log("Server failure!", null);
      }

      public void onSuccess(Object result) {
        GWT.log("Saved Field! ", null);
      }
    });
  }


  @UiHandler("buttonCancel")
  void onClick(ClickEvent e) {
    
  }

}
